package com.pxl.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxl.springbootinit.common.ErrorCode;
import com.pxl.springbootinit.exception.BusinessException;
import com.pxl.springbootinit.model.dto.task.UserFeedbackForm;
import com.pxl.springbootinit.model.entity.User;
import com.pxl.springbootinit.model.entity.UserFeedback;
import com.pxl.springbootinit.model.enums.FeedbackStatusEnum;
import com.pxl.springbootinit.model.vo.UserFeedbackVO;
import com.pxl.springbootinit.model.vo.UserVO;
import com.pxl.springbootinit.service.UserFeedbackService;
import com.pxl.springbootinit.mapper.UserFeedbackMapper;
import com.pxl.springbootinit.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author MADAO
* @description 针对表【user_feedback(用户反馈表)】的数据库操作Service实现
* @createDate 2024-12-20 11:59:03
*/
@Service
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper, UserFeedback>
    implements UserFeedbackService{
    @Resource
    private UserService userService;

    @Override
    public Boolean createUserFeedback(UserFeedbackForm userFeedbackForm) {
        userFeedbackValid(userFeedbackForm);
        UserFeedback userFeedback = new UserFeedback();
        BeanUtils.copyProperties(userFeedbackForm,userFeedback);
        boolean result = this.save(userFeedback);
        if (!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"创建用户反馈失败,请重试");
        }
        return true;
    }

    @Override
    public Boolean updateUserFeedback(UserFeedbackForm userFeedbackForm) {
        userFeedbackValid(userFeedbackForm);
        UserFeedback userFeedback = new UserFeedback();
        BeanUtils.copyProperties(userFeedbackForm,userFeedback);
        boolean result = this.updateById(userFeedback);
        if (!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"更新用户反馈失败,请重试");
        }
        return true;
    }

    @Override
    public Page<UserFeedbackVO> pageUserFeedback(UserFeedbackForm userFeedbackForm) {
        if (userFeedbackForm == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<UserFeedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( userFeedbackForm.getUserId()!= null,"user_id",userFeedbackForm.getUserId());
        queryWrapper.eq( userFeedbackForm.getFeedbackType()!= null,"feedback_type",userFeedbackForm.getFeedbackType());
        queryWrapper.eq( userFeedbackForm.getContent()!= null,"content",userFeedbackForm.getContent());
        queryWrapper.orderByDesc("updated_time");
        Page<UserFeedback> page = this.page(new Page<>(userFeedbackForm.getCurrent(), userFeedbackForm.getPageSize()), queryWrapper);
        Page<UserFeedbackVO> userFeedbackVOPage = new Page<>();
        userFeedbackVOPage.setCurrent(page.getCurrent());
        userFeedbackVOPage.setPages(page.getPages());
        userFeedbackVOPage.setTotal(page.getTotal());
        List<UserFeedbackVO> collect = page.getRecords().stream().map(this::getUserFeedbackVO).collect(Collectors.toList());
        userFeedbackVOPage.setRecords(collect);
        return userFeedbackVOPage;
    }

    /**
     * 基础属性的校验
     * @param userFeedbackForm
     */
    private void userFeedbackValid(UserFeedbackForm userFeedbackForm) {
        if (userFeedbackForm == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long userId = userFeedbackForm.getUserId();
        User user = userService.getById(userId);
        if (user == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户不存在");
        }
    }

    @Override
    public UserFeedbackVO getUserFeedbackVO(UserFeedback userFeedback) {
        UserFeedbackVO userFeedbackVO = new UserFeedbackVO();
        BeanUtils.copyProperties(userFeedback,userFeedbackVO);
        Long userId = userFeedback.getUserId();
        User user = userService.getById(userId);
        UserVO userVO = userService.getUserVO(user);
        userFeedbackVO.setUser(userVO);
        if (user != null){
            userFeedbackVO.setUsername(user.getUsername());
            userFeedbackVO.setEmail(user.getEmail());
            userFeedbackVO.setPhone(user.getPhone());
        }
        return userFeedbackVO;
    }

    @Override
    public Boolean answerUserFeedback(UserFeedbackForm userFeedbackForm) {
        UserFeedback userFeedback = new UserFeedback();
        userFeedback.setId(userFeedbackForm.getId());
        userFeedback.setAnswer(userFeedbackForm.getAnswer());
        userFeedback.setStatus(FeedbackStatusEnum.REPLIED.getValue());
        boolean result = this.updateById(userFeedback);
        if (!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"更新用户反馈失败,请重试");
        }
        return true;
    }

    @Override
    public Page<UserFeedbackVO> pageAdminUserFeedback(UserFeedbackForm userFeedbackForm) {
        if (userFeedbackForm == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<UserFeedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( userFeedbackForm.getUserId()!= null,"user_id",userFeedbackForm.getUserId());
        queryWrapper.eq( userFeedbackForm.getFeedbackType()!= null,"feedback_type",userFeedbackForm.getFeedbackType());
        queryWrapper.eq( userFeedbackForm.getContent()!= null,"content",userFeedbackForm.getContent());
        queryWrapper.orderByDesc("updated_time");
        Page<UserFeedback> page = this.page(new Page<>(userFeedbackForm.getCurrent(), userFeedbackForm.getPageSize()), queryWrapper);
        Page<UserFeedbackVO> userFeedbackVOPage = new Page<>();
        userFeedbackVOPage.setCurrent(page.getCurrent());
        userFeedbackVOPage.setPages(page.getPages());
        userFeedbackVOPage.setTotal(page.getTotal());
        List<UserFeedbackVO> collect = page.getRecords().stream().map(this::getUserFeedbackVO).collect(Collectors.toList());
        userFeedbackVOPage.setRecords(collect);
        return userFeedbackVOPage;
    }
}




